package com.android.duininsuoai.fragment.detail;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.duininsuoai.R;
import com.android.duininsuoai.activity.GoodsDetailActivity;
import com.android.duininsuoai.fragment.BaseLazyFragment;
import com.android.duininsuoai.model.auction.AuctionHomeConfig;
import com.android.duininsuoai.model.auction.AuctionHomeConfigCallback;
import com.android.duininsuoai.utils.ConstantsUrl;
import com.android.duininsuoai.utils.HttpUtils;
import com.android.duininsuoai.view.CustomViewpager;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/10/17.
 */

public class Fragment_AuctioningRule extends BaseLazyFragment {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.web_nodata)
    TextView nodata;
    private FragmentActivity mcontext;
    private View view;
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private String auction_id;
    private int position;
    private CustomViewpager viewpager;
    private GoodsDetailActivity mActivity;
    /**
     * 最新成交
     */

    public void  SecurityInfoFragment(CustomViewpager viewpager, int position, String auction_id)
    {
        this.position = position;
        this.viewpager = viewpager;
        this.auction_id = auction_id;
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = (GoodsDetailActivity) activity;
        mActivity.setHandler2(mHandler);
    }
    public Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 3:
                    mActivity.notifyFinish();
                    break;
            }
        };

    };
    public Fragment_AuctioningRule() {


    }

    public static Fragment_Detail_GoodsDetail_DetailWeb newInstance(String param1) {
        Fragment_Detail_GoodsDetail_DetailWeb fragment = new Fragment_Detail_GoodsDetail_DetailWeb();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {
        GetData();
    }


    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_detail_goods_detail_web, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        viewpager.setObjectForPosition(view,position);

        return view;
    }

    /**
     * 活动商品详情
     */
    private void GetData() {

        final String address = ConstantsUrl.domain + ConstantsUrl.ACUTIONCONFIG ;
        HashMap<String,String> map = new HashMap<>();
            HttpUtils.get(address,null, new AuctionHomeConfigCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    mActivity.notifyFinish();
                }

                @Override
                public void onResponse(AuctionHomeConfig response, int id) {

                    if (response.getCode() == 0) {
                        mParam1 = response.getData().getAuction_rule();
                           initUI();
                        mActivity.notifyFinish();
                        } else {
                            mActivity.notifyFinish();
                        }
                }
            });
    }


    private void initUI() {

        if (mParam1 == null || mParam1.equals("")) {
            progress.setVisibility(View.GONE);
            nodata.setVisibility(View.VISIBLE);

        } else {

            webView.setWebViewClient(new MyWebViewClient());
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                    if (newProgress == 100) {
                        progress.setVisibility(View.GONE);
                    }
                }
            });
            nodata.setVisibility(View.GONE);
            //宽度适应屏幕
            String js = "<style>\n" +
                    "img{\n" +
                    " max-width:100%;\n" +
                    "height:auto\n" +
                    "}\n" +
                    "</style>" + mParam1;

            webView.loadDataWithBaseURL(null, js,"text/html", "utf-8", null);
        }


    }


    class MyWebViewClient extends WebViewClient {
        //拦截网址
    }


}

